package com.cn.demo.springboot.albaba.pool;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
@Configuration
@EnableConfigurationProperties(value = DruidConfigProperties.class)
@EnableTransactionManagement
public class DruidConfig {
	
	private final static Logger log = LoggerFactory.getLogger(DruidConfig.class);
	
	@Autowired
	private DruidConfigProperties druidConfigProperties;

	@Bean
	public ServletRegistrationBean servletRegistrationBean() {
		ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet());
		registration.addUrlMappings("/druid/*");
		registration.addInitParameter("logSlowSql", "true");
		return registration;
	}

	@Bean
	public FilterRegistrationBean filterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		filterRegistrationBean.addUrlPatterns("/*");
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
		filterRegistrationBean.addInitParameter("profileEnable", "true");
		return filterRegistrationBean;
	}

	@Bean("dataSource")
	public DataSource druidDataSource() {
		DruidDataSource datasource = new DruidDataSource();
		datasource.setUrl(druidConfigProperties.getUrl());
		datasource.setUsername(druidConfigProperties.getUsername());
		datasource.setPassword(druidConfigProperties.getPassword());
		datasource.setDriverClassName(druidConfigProperties.getDriverClassName());
		datasource.setInitialSize(druidConfigProperties.getInitialSize());
		datasource.setMinIdle(druidConfigProperties.getMinIdle());
		datasource.setMaxActive(druidConfigProperties.getMaxActive());
		datasource.setMaxWait(druidConfigProperties.getMaxWait());
		datasource.setTimeBetweenEvictionRunsMillis(druidConfigProperties.getTimeBetweenEvictionRunsMillis());
		datasource.setMinEvictableIdleTimeMillis(druidConfigProperties.getMinEvictableIdleTimeMillis());
		datasource.setValidationQuery(druidConfigProperties.getValidationQuery());
		datasource.setTestWhileIdle(druidConfigProperties.isTestWhileIdle());
		datasource.setTestOnBorrow(druidConfigProperties.isTestOnBorrow());
		datasource.setTestOnReturn(druidConfigProperties.isTestOnReturn());
		datasource.setDbType(druidConfigProperties.getType()/*"mysql"*/);
		datasource.setPoolPreparedStatements(druidConfigProperties.isPoolPreparedStatements());
		datasource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigProperties.getMaxPoolPreparedStatementPerConnectionSize());
		datasource.setConnectionProperties(druidConfigProperties.getConnectionProperties());
		try {
			datasource.setFilters(druidConfigProperties.getFilters());
		} catch (SQLException e) {
			log.error("druid configuration initialization filter", e);
		}
		return datasource;
	}

}
